<?php
/**
 * 验证类
 * @author solar
 */
class Validator
{
	/**
	 * @var array list of built-in validators (name=>class)
	 */
	public $builtInValidators=array(
		'required'=>'RequiredValidator',
		'length'=>'LengthValidator',
		'email'=>'EmailValidator',
		'url'=>'UrlValidator',
		'ip'=>'IpValidator',
		'date'=>'DateValidator',
		'boolean'=>'BooleanValidator',
		'int'=>'IntValidator',
		'float'=>'FloatValidator',
		'number'=>'NumberValidator',
		'among'=>'AmongValidator',	
		'match'=>'MatchValidator'
	);
	private $aIncludedValidator=array();
	private $rules=array();
	public $aError=array();
	private $isArrayError=true;
	private $isSkipError=true;	

	function __construct($isArrayError=false,$isSkipError=false){
		$this->isArrayError=$isArrayError;
		$this->isSkipError=$isSkipError;
	}
	
	public function reset(){
		$this->rules=array();
		$this->aError=array();
	}
	
	public function setIsArrayError($isArrayError){
		$this->isArrayError = $isArrayError;
	}
	
	public function setIsSkipError($isSkipError){
		$this->isSkipError = $isSkipError;
	}

	public function setRule($attr,$rule){
		$this->rules[$attr]=$rule;
	}

	public function appendRules($rules){
		$this->rules=array_merge($this->rules,$rules);
	}

	public function setRules($rules){
		$this->rules=$rules;
	}
	
	public function getErrors(){
		return $this->isArrayError===true ? $this->aError : implode(';',$this->aError);
	}
	
	/**
	 * 验证方法
	 * @param array $aData
	 * @author solar
	 */
	public function validate($aData){
		/*if(!is_array($aData)) return '参数必须是数组';
		if($aData===array()) return '不接受空数组的验证';*/
		if(!is_array($aData)) $aData = array();
		if(empty($this->rules)) return '规则数组为空，无法验证';
		foreach($this->rules as $key=>$aRule){
			$label=array_shift($aRule);
			if(!array_key_exists($key,$aData)){
				$this->aError[$key]='请输入'.$label;;				
				if(!$this->isSkipError) return $this->getErrors();
			}
			foreach($aRule as $k=>$v){
				if(is_array($v)){
					$name=$k; $aParam=$v;
				}else{
					$name=$v; $aParam=array();
				}				
				if(!in_array($name,$this->aIncludedValidator)){					
					if(!array_key_exists($name,$this->builtInValidators)) return '无法识别的验证器：'.$name;
					include_once PATH_CORE.'/validator/'.$this->builtInValidators[$name].'.php';
					$this->aIncludedValidator[]=$name;
				}
				$oValidator=new $this->builtInValidators[$name];
				foreach($aParam as $attr=>$param){
					$oValidator->$attr=$param;
				}
				$result=$oValidator->validate($label,$aData[$key]);
				if($result===true) continue;
				$this->aError[$key]=$result;		
				if(!$this->isSkipError) return $this->getErrors();				
			}
		}
		if($this->aError===array()) return true;
		else return $this->getErrors();
	}
	
	/**
	 * 验证单个值
	 * @param string $key
	 * @param mix $value
	 * @param array $rule
	 */
	public function validateOne($key,$value,$rule,$isArrayError=false) {
		$this->reset();
		$this->setIsSkipError(false);
		$this->setIsArrayError($isArrayError);		
		$this->setRule($key,$rule);
		return $this->validate(array($key=>$value));
	}
		
}
